/**
 * 
 */
package org.dragonfire.dasm.postprocessor;

import java.util.ArrayList;

import org.dragonfire.dasm.CompileException;
import org.dragonfire.dasm.DASM;
import org.dragonfire.dasm.Listing;

/**
 * @author dandroid
 * @date Dec 8, 2006
 */
public class Postprocessor {

	private DASM compiler;
	private Listing currListing = null;

	public int getPC() {
		return currListing.getPc();
	}

	public Postprocessor(DASM comp) {
		compiler = comp;
	}

	public void process(ArrayList<Listing> listing) {
		for (Listing l : listing) {
			currListing = l;
			try {
				if (l.getInstruction() != null) {
					l.getInstruction().pass2(l, l.getCommand());
				}
				else if (l.getCommand().isDirective()) {
					getCompiler().getDM().pass2(l);
				}
			} catch (CompileException ce) {
				getCompiler().prepCompileException(ce);
				getCompiler().error(ce);
			} catch (Throwable e) {
				CompileException ce = new CompileException("ERROR_INTERNAL", DASM.AUTHOR);
				getCompiler().prepCompileException(ce);
				getCompiler().error(ce);
				if (getCompiler().getBooleanParam("debug")) {
					System.err.println("Pass 2:");
					e.printStackTrace(System.err);
				}
			}
		}
		currListing = null;
	}

	public int getCurrentLineNum() {
		if (currListing == null)
			return -1;
		return currListing.getLineNum();
	}

	public String[] getCurrentModuleStack() {
		return getCurrentListing().getModuleStack();
	}

	public String getCurrentFileName() {
		if (currListing == null)
			return "<Unknown>";
		return currListing.getFileName();
	}

	public Listing getCurrentListing() {
		return currListing;
	}

	public String getCurrentModule() {
		return currListing.getModule();
	}

	public String eval(String equ) {
		return getCompiler().eval(equ);
	}

	public DASM getCompiler() {
		return compiler;
	}
}
